package bsh.asm.acessor;

import org.objectweb.asm.MethodVisitor;

import bsh.CompilationState;
import bsh.exception.CompilationAssertionFailed;
import bsh.meta.TypeWrapper;
import bsh.meta.UnfinishedType;

/**
 * {@link ObjectAccessor} para o 'this'
 * 
 * @author takeshi
 */
public class ThisObjectAccessor implements ObjectAccessor {

    private final UnfinishedType type;

    /**
     * @param type {@link UnfinishedType}
     */
    public ThisObjectAccessor(UnfinishedType type) {
        super();
        this.type = type;
    }

    /**
     * {@inheritDoc}
     */
    public void loadObject(MethodVisitor visitor, CompilationState state) {
        type.loadThis(visitor, state);
    }

    /**
     * {@inheritDoc}
     */
    public TypeWrapper type() {
        return this.type;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return "this";
    }

    /**
     * {@inheritDoc}
     */
    public void preStore(MethodVisitor visitor, CompilationState state) {
        throw new CompilationAssertionFailed("THIS is readonly");
    }

    /**
     * {@inheritDoc}
     */
    public boolean readOnly() {
        return true;
    }

    /**
     * {@inheritDoc}
     */
    public void storeObject(MethodVisitor visitor, CompilationState state) {
        throw new CompilationAssertionFailed("THIS is readonly");
    }

}
